<!--
 * @Description: 你的代码我的心
 * @Author: GGB
 * @Date: 2021-12-24 14:08:47
 * @LastEditors: GGB
 * @LastEditTime: 2022-02-22 10:59:22
-->
<template>
  <BasicModal v-bind="$attrs" @ok="handleOk" @register="register" title="物流发货">
    <!-- 表单信息 -->
    <BasicForm class="p-4" @register="formRegister" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { deliverySchemas } from './modal.data';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { delivery } from '/@/api/order/event';
  import { DeliveryType } from '/@/api/order/model/orderModel';
  import { OrderRecordModel } from '../../model';
  // 弹框的数据记录

  const record = ref<OrderRecordModel | any>({});
  let reload = () => {};

  //
  const [formRegister, { validate }] = useForm({
    schemas: deliverySchemas,
    layout: 'vertical',
    showActionButtonGroup: false,
    actionColOptions: {
      span: 24,
    },
  });
  //  初始化弹框信息
  const callback = (data) => {
    // 初始化表单
    record.value = data.record;
    reload = data.reload;
  };
  const [register, { closeModal, changeOkLoading }] = useModalInner(callback);

  // 更改信息
  const handleOk = () => {
    validate().then((res) => {
      if (!res.errorFields) {
        let param = {
          orderId: record.value.order_id,
          form: res,
        } as DeliveryType;
        changeOkLoading(true);
        const { createMessage } = useMessage();
        delivery(param)
          .then(() => {
            createMessage.success('发货成功');
            reload();
            closeModal();
          })
          .catch(() => {
            createMessage.error('发货失败');
          })
          .finally(() => {
            changeOkLoading(false);
          });
      }
    });
  };
</script>

<style lang="less" scoped>
  .container {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 200px;
  }
</style>
